(* Initialisation de GTK. *)
let _ = GMain.init ()

let path = ImageProcessing.path

let interv = ref 10
(* Fenêtre principale de l'application. *)
let window = 
  GWindow.window 
    ~title:"Ramani Ya Mradi" 
    ~position:`CENTER
    ~resizable:true ()

(* pré-Box pour widgets *)
let wbox = GPack.vbox
  ~spacing:10
  ~border_width:10
  ~homogeneous:false
  ~packing:window#add ()

(* Menu *)
let menu = GMenu.menu_bar
  ~packing:wbox#pack ()

(* Onglets *)
let table = GPack.table
  ~rows:3
  ~columns:3
  ~packing:(wbox#pack ~expand:true) ()

let notebook = GPack.notebook
  ~packing:(table#attach
	      ~left:1
	      ~right:2
	      ~top:1
	      ~fill: `BOTH
	      ~expand: `BOTH) ()

let tab_2d =
  let label = GMisc.label
    ~text:("2D") () in
    GPack.vbox
      ~border_width:10
      ~packing:(fun o -> ignore(notebook#append_page
				  ~tab_label:label#coerce o)) ()
let tab_3d =
  let label = GMisc.label
    ~text:("3D") () in
    GPack.vbox
      ~border_width:10
      ~packing:(fun o -> ignore(notebook#append_page
                                  ~tab_label:label#coerce o)) () 
  
let frame_i = GBin.frame
  ~border_width:10
  ~label:"Affichage 2D"
  ~packing:(tab_2d#pack ~expand:true)()

let box2d = GPack.hbox
  ~packing:frame_i#add ()

let frame_3d = GBin.frame
  ~border_width:10
  ~label:"Affichage 3D"
  ~packing:(tab_3d#pack ~expand:true)()

let box3d =GPack.vbox
  ~packing:frame_3d#add ()	     

let vgl =
  GlGtk.area [`DOUBLEBUFFER; `RGBA; `DEPTH_SIZE 16; `BUFFER_SIZE 16] 
      ~width:700
      ~height:500
      ~packing:box3d#add ()

(*let disp =
  Init3d.displayw vgl 700 500*)

(* Box image *)
let ibox = GMisc.image
  ~packing:box2d#add ()
  ~file:"RyR.png"

(* Séléction de fichier *)
let set_file file () =
  path := file#filename;
  ibox#set_file !path;
  file#destroy; ()

let file_selec () =
  let file_sel = GWindow.file_selection
    ~title:"Ramani - Select a map"
    ~position:`CENTER_ON_PARENT
    ~filename:"" ()
  in
    ignore(file_sel#cancel_button#connect#clicked ~callback:file_sel#destroy);
    ignore(file_sel#ok_button#connect#clicked ~callback:(fun () ->
    begin
      set_file file_sel ();
      file_sel#destroy ();
      ()
    end));
  file_sel#show ();
  ()

(* Changement d'image  *)
let load_image () =
  if (Sys.file_exists "map_grid.bmp") then
    ibox#set_file "map_grid.bmp"

let sample () =
  begin
    if (Sys.file_exists "map.obj") then
      Sys.remove "map.obj";
    ImageProcessing.init_image_processing !path;
(* ImageProcessing.creat_interval_list (); *)
    ImageProcessing.draw_contour !interv;
    ImageProcessing.draw_grid ();
    Sampling.sample ();
    load_image ();
    ignore(ObjReader.init_tab "map.obj");
    ()
  end
    
let init3d () =
  begin
    Init3d.init_3d ();
    (*Init3d.displayw vgl 700 500;*)
    ();
  end
    
let frame_s () =
  begin
    let frame = GBin.frame
      ~border_width:10
      ~label:"Interval"
      ~packing:box2d#add () in
    let scale =
      GRange.scale `VERTICAL
	~adjustment: (GData.adjustment
			~lower:5. ~upper:50. ())
	~packing:frame#add ()
    in
      ignore(scale#connect#value_changed begin fun () ->
	begin
	  interv := int_of_float(scale#adjustment#value);
	  ();
	end
      end);
	();
  end
      
let credit () =
      begin
	let tw = GWindow.window
	  ~title:"Credits"
	  ~position:`CENTER
	  ~resizable:true () in
	  tw#show ();
	  let tbox = GPack.vbox 
	    ~spacing:10
	    ~border_width:10
	    ~homogeneous:false
	    ~packing:tw#add ()
	  in
	    ignore(GMisc.label
		     ~text:"CREDITS TO : \n russian_t \n chinois_c \n chicanos_a"
		     ~packing:tbox#add ());
	    ();
      end

(* Config du menu *)
let main_menu = new GMenu.factory menu

let file_menu = main_menu#add_submenu "File"
let edit_menu = main_menu#add_submenu "Edit"
let option_menu = main_menu#add_submenu "More"
(*let help_menu = main_menu#add_submenu "Help"
let about_menu = main_menu#add_submenu "About"*)

let file_sub = new GMenu.factory file_menu
let sub_menu = file_sub#add_item "Open" ~callback:file_selec
let sub_menu = file_sub#add_item "Quit" ~callback:window#destroy

let edit_sub = new GMenu.factory edit_menu
let sub_menu = edit_sub#add_item "2D Processing"
  ~callback:sample
(*let switch_menu = edit_sub#add_item "2D Processed Image"
  ~callback:load_image
let sub_menu = edit_sub#add_item "2D Sampling"
  ~callback:sample*)
let sub_menu = edit_sub#add_item "3D Processing"
  ~callback:init3d
let option_sub = new GMenu.factory option_menu
let sub_menu = option_sub#add_item "Credits"
  ~callback:credit
let sub_menu = option_sub#add_item "Interval"
  ~callback:frame_s
  
(*let help_sub = new GMenu.factory help_menu
  let sub_menu = help_sub#add_item "Manual"
  
  let about_sub = new GMenu.factory about_menu
  let sub_menu = about_sub#add_item "About us"*)
  
(* test pour du texte *)
(*let trytest =
  GText.view
  ~editable:true
  ~packing:wbox#add ()*)
  
(* Main *)
let _ =
  ignore(window#connect#destroy ~callback:GMain.quit);
  window#show ();
  GMain.main ();
